package com.example.home.hospital.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.hospital.activity.HospitalListDetail;
import com.example.home.hospital.bean.HospitalListBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class HospitalListAdapter extends RecyclerView.Adapter<HospitalListAdapter.MyHoldy> {

    private Context context;
    private List<HospitalListBean.RowsBean> list;
    private View view;

    public HospitalListAdapter(Context context, List<HospitalListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_general,null);
        return new MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        //全市知名医院图片、名称、星级等信息
        Glide.with(context).load(ip + list.get(position).getImgUrl()).into(holder.cover);
        holder.title.setText(list.get(position).getHospitalName());
        HospitalListBean.RowsBean currentItem = list.get(position);
        if (currentItem != null) {
            String brief = currentItem.getBrief();
            if (!TextUtils.isEmpty(brief) && brief.length() >= 30) {
                // 使用 Html.fromHtml 处理 HTML 字符串
                CharSequence formattedText = Html.fromHtml(brief.substring(0, 30) + "....");

                // 设置处理后的文本到 TextView
                holder.content.setText(formattedText);
            } else {
                holder.content.setText(brief);
            }
        }
        holder.time.setText("星级："+list.get(position).getLevel());
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,HospitalListDetail.class);
                intent.putExtra("detailId",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private RelativeLayout rootView;
        private ImageView cover;
        private TextView title;
        private TextView content;
        private TextView time;
        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            cover = itemView.findViewById(R.id.cover);
            title = itemView.findViewById(R.id.title);
            content = itemView.findViewById(R.id.content);
            time = itemView.findViewById(R.id.time);

        }
    }
}

